Skip to content

refactor: use sourcegraph/run for command execution#127

Merged
unknwon merged 14 commits intov2from
use-sourcegraph-run
Feb 15, 2026
Merged

refactor: use sourcegraph/run for command execution#127
unknwon merged 14 commits intov2from
use-sourcegraph-run

Conversation

@unknwon
Copy link
Member

@unknwon unknwon commented Feb 14, 2026

Summary

  • Replace custom os/exec-based command execution in command.go with github.com/sourcegraph/run, delegating process lifecycle management to the library
  • Preserve the entire public API surface (RunInDir, RunInDirPipeline, RunInDirWithOptions, Run) and all behavioral contracts (default timeout, cancellation → context.Canceled, deadline → ErrExecTimeout, stderr in errors, debug logging)
  • Use run.Arg() to properly quote arguments for the library's shell-based parser, and Output.Stream() for raw byte-preserving output (important for binary-ish output like ls-tree -z)

All existing tests pass without modification.

Replace custom os/exec-based command execution with
github.com/sourcegraph/run. The new implementation delegates process
lifecycle management to the library while preserving the existing public
API and all behavioral contracts (timeout, cancellation, stderr
handling, debug logging).

Key changes in command.go:
- Replace exec.CommandContext with run.Cmd, using run.Arg to quote
  arguments for the shell-based parser
- Add newRunCmd helper to centralize run.Command construction
- Simplify RunInDirWithOptions by streaming stdout via Output.Stream
  and extracting stderr from the error
- Optimize RunInDir to stream directly to a bytes.Buffer, preserving
  raw bytes (important for binary-ish output like ls-tree -z)
@unknwon unknwon requested a review from a team as a code owner February 14, 2026 17:18
Convert repo_tree.go LsTree to use gitRun with local args, removing
the last NewCommand call site. Rewrite command_test.go to test the
new gitRun/gitPipeline helpers directly, removing tests for the
deleted Command struct.
Use opt.Args/opt.Envs instead of opt.CommandOptions.Args/opt.CommandOptions.Envs
since CommandOptions is an embedded field.
unknwon

This comment was marked as off-topic.

unknwon

This comment was marked as off-topic.

unknwon

This comment was marked as resolved.

unknwon

This comment was marked as outdated.

unknwon

This comment was marked as outdated.

unknwon

This comment was marked as off-topic.

unknwon

This comment was marked as off-topic.

unknwon

This comment was marked as resolved.

Fix timeout context cancel leakage, tighten exit-status matching, remove fragile
stderr re-concatenation paths, and harden author/diff argument handling.
@unknwon

This comment was marked as resolved.

unknwon

This comment was marked as resolved.

- Fix exit status matching: replace loose HasPrefix with isExitStatus
  helper that won't match "exit status 1" against "exit status 128"
- Fix opt.Args ordering in ShowRefVerify and DeleteTag: move before
  --end-of-options so flag args work correctly
- Fix gitRun log capture: capture partial stdout on error instead of
  logging empty output for failed commands
- Quote args with special characters in log output for readability
- Fix repo_pull_test: "bad_revision" causes exit 128, not exit 1

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Remove nil check on ctx in gitCmd (callers always provide non-nil ctx)
- Remove stdin parameter from gitPipeline (no production caller uses it)
- Rewrite isExitStatus to use run.ExitCoder interface instead of string
  parsing, as suggested by reviewer
- Remove unused errors and os/exec imports
- Update tests to use gitCmd directly for stdin-based scenarios

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
unknwon and others added 3 commits February 14, 2026 20:57
- Rename helpers: gitCmd -> cmd, gitRun -> exec, gitPipeline -> pipe
- Remove stderr parameter from pipe (stderr is already in the error)
- Remove extractStderr (no longer used after stderr removal)
- Remove Blob.Pipeline stderr parameter and update all callers
- Rename CreateArchive -> Archive and add --end-of-options

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@unknwon unknwon merged commit 78fda40 into v2 Feb 15, 2026
4 checks passed
@unknwon unknwon deleted the use-sourcegraph-run branch February 15, 2026 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments